package be.ac.ua.comp.projectGPT.Main;

//~--- non-JDK imports --------------------------------------------------------

import be.ua.ac.comp.projectGPT.auctionPolicies.BiddingRoundPolicy;
import be.ac.ua.comp.projectGPT.BiddingPolicies.IntervalPolicy;
import be.ac.ua.comp.projectGPT.BiddingPolicies.MaximumBidPolicy;
import be.ac.ua.comp.projectGPT.Jobs.JobGenerator;
import be.ac.ua.comp.projectGPT.Jobs.implementations.BusyLoopJob;
import be.ac.ua.comp.projectGPT.Jobs.implementations.FileCompareJob;
import be.ac.ua.comp.projectGPT.Jobs.implementations.SleepingJob;
import be.ac.ua.comp.projectGPT.auctions.EnglishAuction;
import be.ac.ua.comp.projectGPT.biddingAgents.BiddingAgent;
import be.ac.ua.comp.projectGPT.biddingAgents.EnglishBiddingAgent;
import be.ac.ua.comp.projectGPT.exceptions.DuplicateException;
import be.ac.ua.comp.projectGPT.exceptions.InitializationException;
import be.ac.ua.comp.projectGPT.players.Bank;
import be.ac.ua.comp.projectGPT.players.Consumer;
import be.ac.ua.comp.projectGPT.players.Market;
import be.ac.ua.comp.projectGPT.players.Provider;
import be.ua.ac.comp.projectGPT.auctionPolicies.BiddingStopPolicy;
import java.lang.reflect.InvocationTargetException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Test driver.
 */
public class AuctionTest {
    final static Logger logger = LoggerFactory.getLogger(AuctionTest.class.getSimpleName());

    public static void main(String[] args) throws InitializationException, InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchMethodException, InvocationTargetException {
        try {

            logger.info("Starting test");

            /** Creating a market */
            Market         market          = new Market();


            /** Creating a bank */
            Bank bank = new Bank();

            /** Creating English auctions */
            EnglishAuction englishAuction1 = market.getEnglishAuction("Wilfred", new BiddingRoundPolicy(5),
                                                 new IntervalPolicy(50));
            EnglishAuction englishAuction2 = market.getEnglishAuction("James", new BiddingStopPolicy(),
                                                 new MaximumBidPolicy(500));

            /** Creating providers */
            Provider       providerMarc     = new Provider("Marc", englishAuction1, bank);
            Provider       providerJohn     = new Provider("John", englishAuction2,bank);

            /** Creating consumers */
            Consumer       consumerDirk     = new Consumer("Dirk", 100,bank);

            consumerDirk.addJob(new SleepingJob(200));
            consumerDirk.addJob(new BusyLoopJob(20));

            Consumer       consumerJames    = new Consumer("James", 200,bank);
            consumerJames.addJob(new FileCompareJob("holala","holala"));
            consumerJames.addJob(new SleepingJob(20));

            Consumer consumerJan = new Consumer("Jan",300,bank);
            consumerJan.addJob(new SleepingJob(50));
            consumerJan.addJob(new BusyLoopJob(20));

            Consumer consumerZordon = new Consumer("Zordon",400,bank);
            consumerZordon.addJob(new SleepingJob(50));
            consumerZordon.addJob(new BusyLoopJob(20));
  
            BiddingAgent   biddingAgentJack = new EnglishBiddingAgent("Jack", consumerDirk);
            biddingAgentJack.setAuction(englishAuction2);

            BiddingAgent   biddingAgentAlex = new EnglishBiddingAgent("Alex", consumerJames);
            biddingAgentAlex.setAuction(englishAuction1);

            BiddingAgent biddingAgentKarel = new EnglishBiddingAgent("Karel",consumerJan);
            biddingAgentKarel.setAuction(englishAuction2);

            BiddingAgent biddingAgentJef = new EnglishBiddingAgent("Jef",consumerZordon);
            biddingAgentJef.setAuction(englishAuction2);

            englishAuction1.addBiddingAgent(biddingAgentAlex);
            englishAuction1.addBiddingAgent(biddingAgentJack);

            englishAuction2.addBiddingAgent(biddingAgentKarel);
            englishAuction2.addBiddingAgent(biddingAgentJef);


            //market.startAuction(englishAuction1);

           

            market.startAuction(englishAuction1);
            market.startAuction(englishAuction1);
            
            //market.startAuction(englishAuction2);
            Thread.sleep(200);
            providerMarc.stopJobs();
            market.endAuctions();

            consumerDirk.printStatistics();
            consumerJames.printStatistics();

        } catch (DuplicateException ex) {
            logger.error("Duplicate Exception: "+ex.getLocalizedMessage());
        }
    }
}
